package com.bg.sparkproject.pattern.single.slacker;

/**
 * @Auther: zhengshunzhi
 * @Date: 2018/12/18 11:54
 * @Description: 单例模式 懒汉式
 */
public class Singleton {

    //私有化实例
    private static volatile Singleton singleton = null;

    private Singleton() {
        System.out.println("单例模式懒汉式创建");
    }

    //公有静态方法提供实例供外界访问
    public static Singleton getInstance() {
        if (singleton == null) {
            //防止多线程重复创建
            //double check双重检查
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}
